﻿Imports System.Web.Mvc
Imports System.Net
Imports System.IO
Imports Newtonsoft.Json.Linq

Public Class MovieController
    Inherits Controller

    Private mdMovie As MovieViewModel
    'Private Const ApiKey As String = "07729bef670a78e80c07b39ba1f3ec7a"
    Private Const ApiKey As String = "470fd2ec8853e25d2f8d86f685d2270e"

    ' GET: /Movie
    Public Function Index() As ActionResult
        Return View()
    End Function

    Public Function GetMoviesData(ByVal ShowType As String) As JsonResult

        Dim xUrl As String = "http://api.themoviedb.org/3/movie/"
        Dim xApiKey As String = String.Empty

        Try
            If ShowType Is Nothing Then
                Return Nothing
            End If
            xApiKey = "?api_key=" & ApiKey

            xUrl = xUrl & ShowType & xApiKey

            Dim request = TryCast(System.Net.WebRequest.Create(xUrl), System.Net.HttpWebRequest)
            request.Method = "GET"
            request.Accept = "application/json"
            request.ContentLength = 0
            Dim responseContent As String

            Using response = TryCast(request.GetResponse(), System.Net.HttpWebResponse)
                Using reader = New System.IO.StreamReader(response.GetResponseStream())
                    responseContent = reader.ReadToEnd()
                End Using
            End Using

            Return Json(responseContent, JsonRequestBehavior.AllowGet)

        Catch ex As Exception
            MsgBox(ex.ToString)
            Return Nothing
        End Try

    End Function
End Class